import type { Router } from 'vue-router';
import { createRouter, createWebHistory } from 'vue-router';

import commonRoutes from './common-routes';

type RouterGuardSetter = (router: Router) => void;

const guardModules: Recordable<RouterGuardSetter> = import.meta.glob('./guard/**/*.ts', {
  eager: true,
  import: 'default',
  exhaustive: false,
});

const router = createRouter({
  history: createWebHistory(),
  strict: false,
  scrollBehavior: () => ({ left: 0, top: 0 }),
  routes: commonRoutes,
});

for (const guard of values(guardModules)) {
  guard.call(undefined, router);
}

export default router;
